package Auxiliary;

import java.util.Date;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import Utils.XmlParseHelper;
import Cinema.DataLayer.IXmlParsable;
import Cinema.DataLayer.XmlDefines;
import Exceptions.WrongXMLElement;


/**
 * Represents a credit card information.
 * @author The Fuchtzeinsoft Team.
 *
 */
public class PaymentDetails implements IXmlParsable{

	private long cardNumber;
	private String cardName;
	private Date validDate;
	private int CSC;
	
	/**
	 * Constructor.
	 * @param cardNumber - number of credit card, without spaces or hyphens.
	 * @param cardName - full name of the card owner.
	 * @param validDate  - card expiration date.
	 * @param CSC - card security code (3/4 digits).
	 */
	public PaymentDetails(long cardNumber, String cardName, Date validDate, int CSC) {
		this.cardNumber = cardNumber;
		this.cardName = cardName;
		this.validDate = validDate;
		this.CSC = CSC;
	}
	
	public PaymentDetails(Element element) throws WrongXMLElement {
		initFromXml(element);
	}

	public long getCardNumber() {
		return cardNumber;
	}

	public String getCardName() {
		return cardName;
	}

	public Date getValidDate() {
		return validDate;
	}

	public int getCSC() {
		return CSC;
	}
	
	@Override
	public Element getXml(Document document) {
		
		//create new representing element
		Element resultElement = document.createElement(XmlDefines.PAYMENT);
		
		//fill the element with local data
		XmlParseHelper.appendNewElementToParent(document, resultElement, XmlDefines.CARD_NUMBER, new Long(cardNumber).toString());
		XmlParseHelper.appendNewElementToParent(document, resultElement, XmlDefines.CARD_NAME, cardName);
		XmlParseHelper.appendNewElementToParent(document, resultElement, XmlDefines.DATE, new Long(validDate.getTime()).toString());
		XmlParseHelper.appendNewElementToParent(document, resultElement , XmlDefines.CSC, new Integer(CSC).toString());
		
		return resultElement;
	}
	
	@Override
	public void initFromXml(Element xmlElement) throws WrongXMLElement {
		
		//verifying correct element
		if (!xmlElement.getTagName().equals(XmlDefines.PAYMENT)){
			throw new WrongXMLElement();
		}
		
		//init class
		cardNumber = Long.parseLong(XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.CARD_NUMBER).getTextContent());
		cardName = XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.CARD_NAME).getTextContent();
		validDate = new Date(Long.parseLong(XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.DATE).getTextContent()));
		CSC = Integer.parseInt(XmlParseHelper.getFirstChildByName(xmlElement, XmlDefines.CSC).getTextContent());
		
	}
	
	@Override
	public void initFromXml(boolean isRestorinf, Element xmlElement)
			throws WrongXMLElement {
		initFromXml(xmlElement);
		
	}
}
